using Common;
using Common.NetWork;

using GRPCDemo.DataBase.Entities;
using GRPCDemo.Models;
using GRPCDemo.Network;
using GRPCDemo.Protocol;

namespace GRPCDemo.Services
{
    public class UserService : Singleton<UserService>
    {
        public void Start()
        {
            MessageDistributer<NetConnection<NetSession>>.Instance.Subscribe<UserLoginRequest>(OnLogin);
            MessageDistributer<NetConnection<NetSession>>.Instance.Subscribe<UserRegistRequest>(OnRegist);
            MessageDistributer<NetConnection<NetSession>>.Instance.Subscribe<UserLoginOutRequest>(OnLoginOut);
        }

        public void Stop()
        {
            MessageDistributer<NetConnection<NetSession>>.Instance.Unsubscribe<UserLoginRequest>(OnLogin);
            MessageDistributer<NetConnection<NetSession>>.Instance.Unsubscribe<UserRegistRequest>(OnRegist);
            MessageDistributer<NetConnection<NetSession>>.Instance.Unsubscribe<UserLoginOutRequest>(OnLoginOut);
        }


        private void OnLogin(NetConnection<NetSession> sender, UserLoginRequest request)
        {
            Log.Info($"OnLogin: username:{request.Name}, password:{request.Password}");
            var response = new UserLoginResponse();

            var user = DBService.Instance.Entities.Users.Where(m => m.Name == request.Name).FirstOrDefault();
            if (user != null)
            {
                if (user.LoginStatus)
                {
                    response.Result = Result.Failed;
                    response.errMsg = "用户已登录";
                    Log.Warning($"OnLogin: Response: Result:{response.Result}; errMsg:{response.errMsg}");
                }
                else if (request.Password != user.Password)
                {
                    response.Result = Result.Failed;
                    response.errMsg = "密码错误";
                    Log.Warning($"OnLogin: Response: Result:{response.Result}; errMsg:{response.errMsg}");
                }
                else
                {
                    user.LoginStatus = true;
                    sender.Session.User = new User(user);
                    response.Result = Result.Success;
                    response.User = new NUserInfo
                    {
                        Uid = user.UID,
                        Name = user.Name,
                        Level = user.Level,
                    };
                    DBService.Instance.Save();
                }
            }
            else
            {
                response.Result = Result.Failed;
                response.errMsg = "用户不存在";
                Log.Warning($"OnLogin: Response: Result:{response.Result}; errMsg:{response.errMsg}");
            }

            Log.Info("OnLogin: Return");
            sender.Session.Response.UserLoginResponse = response;
            sender.SendResponse();
        }

        private void OnRegist(NetConnection<NetSession> sender, UserRegistRequest request)
        {
            Log.Info($"OnRegist: username:{request.Name}, password:{request.Password}");
            var response = new UserRegistResponse();

            var user = DBService.Instance.Entities.Users.Where(m => m.Name == request.Name).FirstOrDefault();
            if (user != null)
            {
                response.Result = Result.Failed;
                response.errMsg = "用户已存在";
                Log.Warning($"OnRegist: Response: Result:{response.Result}; errMsg:{response.errMsg}");
            }
            else
            {
                var newUser = new TUser();
                newUser.UID = (DBService.Instance.Entities.Users.Count() + 1).ToString();
                newUser.Name = request.Name;
                newUser.Level = 1;
                newUser.Password = request.Password;
                newUser.Status = true;

                DBService.Instance.Entities.Users.Add(newUser);
                DBService.Instance.Save();
                response.Result = Result.Success;
            }
            Log.Info($"OnRegist: Response: Result:{response.Result}; errMsg:{response.errMsg}");

            Log.Info("OnRegist: Return");
            sender.Session.Response.UserRegistResponse = response;
            sender.SendResponse();
        }

        private void OnLoginOut(NetConnection<NetSession> sender, UserLoginOutRequest request)
        {
            Log.Info($"OnLoginOut: username:{request.Name}");
            var response = new UserLoginOutResponse();

            sender.Session.User = null;
            var user = DBService.Instance.Entities.Users.Where(m => m.Name == request.Name).FirstOrDefault();
            if (user != null)
            {
                user.LoginStatus = false;
                DBService.Instance.Save();
            }

            response.Result= Result.Success;

            sender.Session.Response.UserLoginOutResponse = response;
            sender.SendResponse();
        }
    }
}
